/**!
 *
 * 添加举报。
 *
 */

$(function () {
    /** 举报表单的 jQuery 对象 */
    var $reportForm = $("#report_form");
    /** 举报标题的 jQuery 对象 */
    var $reportName = $reportForm.find("input[name='reportName']");
    /** 举报内容的 jQuery 对象 */
    var $reportContent = $reportForm.find("textarea[name='reportContent']");
    /** 举报人的 jQuery 对象 */
    var $reportreportCreatePerson = $reportForm.find("input[name='reportCreatePerson']");
    /** 举报图片的 jQuery 对象 */
    var $reportImages = $reportForm.find("input[name='reportImages']");
    /** 上传举报图片的 jQuery 对象 */
    var $uploadReportImagesBtn = $("#upload_report_images_btn");
    /** 添加按钮的 jQuery 对象 */
    var $addReportBtn = $reportForm.find("button[name='addReportBtn']");
    /** 举报图片的路径 */
    var reportImages = "";

    // 上传图片
    var $reportImagesUploadifive = $uploadReportImagesBtn.myUploadifive({
        auto: false,
        multi: true,
        width: 135,
        height: 50,
        queueID: "report_queue",
        fileType: "image/*",
        buttonText: "<i class='iconfont icon-upload f-s-14'></i>上传图片",
        buttonClass: "btn btn-danger btn-lg",
        fileObjName: "file",
        uploadScript: "{0}weixin/file/upload.json".format(globalBasePath),
        fileSizeLimit: String(50 * 1024),
        onUploadFn: function () {
            $reportImagesUploadifive.uploadifiveData.settings.formData.path = "/upload/report";
        },
        onUploadCompleteFn: function (file, data) {
            var result = data.result;

            reportImages += (result.filePath + "|");
        },
        onQueueCompleteFn: function () {
            $reportImages.val(reportImages.substring(0, (reportImages.length - 1)));

            // 提交表单
            $reportForm.get(0).submit();
        }
    });

    $addReportBtn.click(function () {
        if ($.isBlank($reportName.val())) {
            $.toast({
                top: "5",
                msg: "请输入举报标题",
                type: "error",
                close: 3000
            });

            $reportName.focus();

            return;
        }

        if ($reportName.val().length > 500) {
            $.toast({
                top: "5",
                msg: "举报标题的长度不能超过500个字",
                type: "error",
                close: 3000
            });

            $reportName.focus();

            return;
        }

        if ($.isBlank($reportContent.val())) {
            $.toast({
                top: "5",
                msg: "请输入举报内容",
                type: "error",
                close: 3000
            });

            $reportContent.focus();

            return;
        }

        if ($reportContent.val().length > 2000) {
            $.toast({
                top: "5",
                msg: "举报内容的长度不能超过2000个字",
                type: "error",
                close: 3000
            });

            $reportContent.focus();

            return;
        }

        if ($.isBlank($reportreportCreatePerson.val())) {
            $.toast({
                top: "5",
                msg: "请输入举报人",
                type: "error",
                close: 3000
            });

            $reportreportCreatePerson.focus();

            return;
        }

        if ($reportreportCreatePerson.val().length > 50) {
            $.toast({
                top: "5",
                msg: "举报人的长度不能超过50个字",
                type: "error",
                close: 3000
            });

            $reportreportCreatePerson.focus();

            return;
        }

        $addReportBtn.html("<i class='fa fa-spinner fa-spin f-s-16'></i>正在提交…").attr({"disabled": "disabled"});

        if ($.isNotNull($reportImagesUploadifive) && ($reportImagesUploadifive.uploadifiveData.queue.count > 0)) {
            // 开始上传文件
            $uploadReportImagesBtn.uploadifive("upload");
        } else {
            // 提交表单
            $reportForm.get(0).submit();
        }
    });
});